/* eslint-disable */
import request from '@/request'

/** 此处后端没有提供注释 POST /scenic/add */
export async function addScenic(data: FormData, options?: { [key: string]: any }) {
  return request<API.BaseResponseLong>('/scenic/add', {
    method: 'POST',
    data,
    ...(options || {}),
  })
}

/** 此处后端没有提供注释 POST /scenic/delete */
export async function deleteScenic(body: API.DeleteRequest, options?: { [key: string]: any }) {
  return request<API.BaseResponseBoolean>('/scenic/delete', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  })
}

/** 此处后端没有提供注释 GET /scenic/list/vo */
export async function getListScenicVo(
  // 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
  params: API.getListScenicVOParams,
  options?: { [key: string]: any },
) {
  return request<API.BaseResponseScenicPageVO>('/scenic/page/list/vo', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  })
}

/** 此处后端没有提供注释 POST /scenic/update */
export async function updateScenic(body: API.updateScenicParams, options?: { [key: string]: any }) {
  console.log('🚀 updateScenic 被调用，参数:', body)

  // 构建FormData，因为后端期望FormData格式
  const formData = new FormData()

  // 添加基本字段
  if (body.id !== undefined) {
    formData.append('id', body.id)
    console.log('📝 添加ID到FormData:', body.id)
  }
  if (body.scenicName !== undefined) {
    formData.append('scenicName', body.scenicName)
    console.log('📝 添加景点名称到FormData:', body.scenicName)
  }
  if (body.scenicBrief !== undefined) {
    formData.append('scenicBrief', body.scenicBrief)
  }
  if (body.tips !== undefined) {
    formData.append('tips', body.tips)
  }
  if (body.scenicLongitude !== undefined) {
    formData.append('scenicLongitude', body.scenicLongitude.toString())
  }
  if (body.scenicLatitude !== undefined) {
    formData.append('scenicLatitude', body.scenicLatitude.toString())
  }

  // 处理tags数组 - 转换为逗号分隔的字符串
  if (body.tags && body.tags.length > 0) {
    formData.append('tags', body.tags.join(','))
    console.log('📝 添加标签到FormData:', body.tags.join(','))
  }

  // 添加封面图片
  if (body.coverImage) {
    formData.append('coverImage', body.coverImage)
    console.log('📸 添加封面图片到FormData:', body.coverImage.name, body.coverImage.size, 'bytes')
  } else {
    console.log('📸 没有封面图片需要上传')
  }

  // 添加要保留的景点图片URL列表
  if (body.keepScenicImages && body.keepScenicImages.length > 0) {
    // 后端期望接收 keepScenicImages 参数
    body.keepScenicImages.forEach((imageUrl: string) => {
      formData.append('keepScenicImages', imageUrl)
    })
    console.log('📝 添加要保留的景点图片到FormData:', body.keepScenicImages)
  } else {
    console.log('📝 没有要保留的原有景点图片')
  }

  // 添加景点图片
  if (body.scenicImages && body.scenicImages.length > 0) {
    console.log('📸 开始添加景点图片到FormData:', body.scenicImages.length, '个文件')
    body.scenicImages.forEach((image: File, index: number) => {
      console.log(`📸 添加景点图片 ${index + 1}:`, {
        name: image.name,
        size: image.size,
        type: image.type,
        lastModified: image.lastModified,
      })
      formData.append('scenicImages', image)
    })
    console.log('✅ 所有景点图片已添加到FormData')
  } else {
    console.log('📸 没有新的景点图片需要上传')
  }

  // 打印FormData内容用于调试
  console.log('📦 FormData 内容总览:')
  let formDataEntryCount = 0
  let fileCount = 0
  let scenicImagesCount = 0

  for (let [key, value] of formData.entries()) {
    formDataEntryCount++
    if (value instanceof File) {
      fileCount++
      if (key === 'scenicImages') {
        scenicImagesCount++
      }
      console.log(`  ${key}: [File] ${value.name} (${value.size} bytes, ${value.type})`)
    } else {
      console.log(`  ${key}: ${value}`)
    }
  }

  console.log(
    `📊 FormData 统计: 总计 ${formDataEntryCount} 个条目, ${fileCount} 个文件, 其中 ${scenicImagesCount} 个景点图片`,
  )

  // 特别检查 scenicImages 参数
  const scenicImagesEntries = []
  for (let [key, value] of formData.entries()) {
    if (key === 'scenicImages') {
      scenicImagesEntries.push(value)
    }
  }
  console.log(`🔍 FormData 中的 scenicImages 参数数量: ${scenicImagesEntries.length}`)

  if (scenicImagesEntries.length === 0 && body.scenicImages && body.scenicImages.length > 0) {
    console.error('❌ 警告: body.scenicImages 有数据但 FormData 中没有 scenicImages 参数!')
    console.error(
      '   body.scenicImages:',
      body.scenicImages.map((f) => f.name),
    )
  }

  return request<API.BaseResponseBoolean>('/scenic/update', {
    method: 'POST',
    data: formData,
    ...(options || {}),
  })
}

/** 推荐景点列表 GET /scenic/recommend/list/vo */
export async function getRecommendScenicList(options?: { [key: string]: any }) {
  return request<API.BaseResponseListScenicVO>('/scenic/recommend/list/vo', {
    method: 'GET',
    ...(options || {}),
  })
}
